//
//  MyKaQuanUnusedViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/17.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "MyKaQuanUnusedViewController.h"
//controller
#import "ShangPinLeiMuViewController.h"
//model
#import "MyWeiYongCouponModel.h"

@interface MyKaQuanUnusedViewController ()

@end

@implementation MyKaQuanUnusedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //点击事件
    FDWeakSelf;
    self.mykaquanBaseVCClickCellBtnBlock = ^{
        ShangPinLeiMuViewController *splmVC = [[ShangPinLeiMuViewController alloc] init];
        [weakSelf.navigationController pushViewController:splmVC animated:YES];
    };
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - 请求数据
- (void)loadData {
    [super loadData];
    
    [FDHttpAPi requestSYUserCouponUsableSuccessBlock:^(id responseObjc) {
        
        [self.tableView.mj_header endRefreshing];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) { //请求成功
            NSArray *weiYongArr = responseObjc[@"data"];
            
            //解析数据
            NSMutableArray *weiyongArrM = [NSMutableArray array];
            
            for (NSDictionary *dic1 in weiYongArr) {
                NSDictionary *couponDic = dic1[@"coupon"];
                //先将coupon转换为model
                MyCouponModel *couponModel = [MyCouponModel mj_objectWithKeyValues:couponDic];
                
                //重新创建一个可变字典
                NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
                [newDic setValue:couponModel forKey:@"coupon"];
                [newDic setValue:dic1[@"id"] forKey:@"id"];
                
                //字典转模型
                MyWeiYongCouponModel *weiYongModel = [MyWeiYongCouponModel mj_objectWithKeyValues:newDic];
                //转入数组
                [weiyongArrM addObject:weiYongModel];
            }
            
            //回到主线程中刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                self.dataArr = weiyongArrM;
                
                if (self.dataArr.count) {
                    self.nodataView.hidden = YES;
                }else {
                    self.nodataView.hidden = NO;
                }
                [self.tableView reloadData];
            });
        }
    } failBlock:^(NSError *error) {
        
        FDLog(@"发生了错误:%@",[error localizedDescription]);
        self.nodataView.hidden = YES;
        [self.tableView.mj_header endRefreshing];
    }];
    
}
@end

